<?php
namespace Admin\Model;
/**
 * ============================================================================
 * WSTMall开源商城
 * 官网地址:http://www.wstmall.net
 * 联系QQ:707563272
 * ============================================================================
 * 订单结算服务类
 */
class OrderSettlementsModel extends BaseModel {
	/**
	 * 获取结算列表
	 */
	public function queryByPage(){
//		$settlementNo = WSTAddslashes(I('settlementNo'));
//		$isFinish = (int)I('isFinish',-1);
        $shopName = I("shopName");
		$areaId1 = (int)I('areaId1');
		$areaId2 = (int)I('areaId2');
		$areaId3 = (int)I('areaId3');
//		$sql = "select os.*,p.shopName from __PREFIX__order_settlements os, __PREFIX__shops p where os.shopId=p.shopId ";

//		if($settlementNo!='')$sql.=" and settlementNo like '%".$settlementNo."%'";
//		if($isFinish>-1)$sql.=" and isFinish=".$isFinish;
//		$sql.=" order by settlementId desc";
//		return  $this->pageQuery($sql,(int)I('p'),30);


       $sql = <<<EOT
SELECT
	s.shopName,b.bankName,s.shopId,s.bankNo,s.bankUserName,
	(
		SELECT
			sum(os1.settlementMoney)
		FROM
			__PREFIX__order_settlements os1
		WHERE
			os1.isFinish = 0 and os1.shopId = os.shopId
	) AS unSettlemtMonney,
(
		SELECT
			sum(os2.settlementMoney)
		FROM
			__PREFIX__order_settlements os2
		WHERE
			os2.isFinish = 1  and os2.shopId = os.shopId
	) AS settlemtMonney

FROM
	__PREFIX__order_settlements os
JOIN __PREFIX__shops s ON os.shopId = s.shopId join __PREFIX__banks b on b.bankId = s.bankId
where 1=1

EOT;

        if($areaId1>0)$sql.=" and s.areaId1=".$areaId1;
        if($areaId2>0)$sql.=" and s.areaId2=".$areaId2;
        if($areaId3>0)$sql.=" and s.areaId3=".$areaId3;
        if(!empty($shopName)) $sql .= " and s.shopName like '%$shopName%'";

        $sql .= " GROUP BY s.shopName ORDER BY os.shopId";

        $settlementShopList = $this->pageQuery($sql,(int)I('p'),10);

        foreach($settlementShopList["root"] as &$settlementShop){
            $shopSettlementOrderList = M("OrderSettlements")->where(array("shopId"=>$settlementShop["shopId"]))->limit(10)->select();
            $settlementShop["orderList"] = $shopSettlementOrderList;
        }

        return $settlementShopList;




	}
	
	/**
	 * 获取订单结算信息
	 */
	public function get(){
		$id = (int)I('id');
		return $this->where('settlementId='.$id)->find();
	}
	/**
	 * 获取结算详情
	 */
	public function getDetail(){
		$id = (int)I('id');
		$sql = "select os.*,p.shopName from __PREFIX__order_settlements os,__PREFIX__shops p where os.shopId=p.shopId and os.settlementId=".$id;
		$rs =  $this->queryRow($sql);
		//获取订单列表
		$sql = "select orderId,orderNo,userName,realTotalMoney,poundageRate,poundageMoney from __PREFIX__orders where settlementId=".$id;
		$rs['List'] = $this->query($sql);
		return $rs;
	}
	
	/**
	 * 结算
	 */
	public function settlement(){
		$id = (int)I('id');
		$rd = array('status'=>-1);
	 	$rs = $this->where('isFinish=0 and settlementId='.$id)->find();
	 	if($rs['settlementId']!=''){
	 		$data = array();
	 		$data['isFinish'] = 1;
	 		$data['finishTime'] = date('Y-m-d H:i:s');
	 		$data['remarks'] = I('content');
	 	    $rss = $this->where("settlementId=".$id)->save($data);
			if(false !== $rss){
				$rd['status']= 1;
			}
	 	}
	 	return $rd;
	}


    /**
     * 获取更多结算订单
     */
    public function getMore(){

       	$settlementNo = WSTAddslashes(I('settlementNo'));
		$isFinish = (int)I('isFinish',-1);
        $shopId = (int)I("get.shopId");
        $sql = <<<EOT
SELECT
	s.shopName,b.bankName,s.shopId,s.bankNo,s.bankUserName,
	(
		SELECT
			sum(os1.settlementMoney)
		FROM
			__PREFIX__order_settlements os1
		WHERE
			os1.isFinish = 0 and os1.shopId = os.shopId
	) AS unSettlemtMonney,
(
		SELECT
			sum(os2.settlementMoney)
		FROM
			__PREFIX__order_settlements os2
		WHERE
			os2.isFinish = 1  and os2.shopId = os.shopId
	) AS settlemtMonney

FROM
	__PREFIX__order_settlements os
JOIN __PREFIX__shops s ON os.shopId = s.shopId join __PREFIX__banks b on b.bankId = s.bankId
WHERE os.shopId = $shopId
ORDER BY os.settlementId
EOT;



        $shopSettlementInfo = $this->queryRow($sql);

        $orderSql = <<<EOT
select * from __PREFIX__order_settlements where shopId = $shopId
EOT;

        if($settlementNo!='')$orderSql.=" and settlementNo like '%".$settlementNo."%'";
		if($isFinish>-1)$orderSql.=" and isFinish=".$isFinish;
        $orderSql .= " order by settlementId";
        $shopSettlementOrderList = $this->pageQuery($orderSql,(int)I("p"),20);
        $shopSettlementOrderList["shopSettlement"] = $shopSettlementInfo;
        return $shopSettlementOrderList;

    }

    /**
     * 获取导出的结算数据
     */
    function getExportData(){
        $shopId = (int)I("get.shopId");
        $sql = <<<EOT
SELECT
	s.shopName,b.bankName,s.shopId,s.bankNo,s.bankUserName,
	(
		SELECT
			sum(os1.settlementMoney)
		FROM
			__PREFIX__order_settlements os1
		WHERE
			os1.isFinish = 0 and os1.shopId = os.shopId
	) AS unSettlemtMonney,
(
		SELECT
			sum(os2.settlementMoney)
		FROM
			__PREFIX__order_settlements os2
		WHERE
			os2.isFinish = 1  and os2.shopId = os.shopId
	) AS settlemtMonney

FROM
	__PREFIX__order_settlements os
JOIN __PREFIX__shops s ON os.shopId = s.shopId join __PREFIX__banks b on b.bankId = s.bankId
WHERE os.shopId = $shopId
ORDER BY os.settlementId
EOT;


        $shopSettlementInfo = $this->queryRow($sql);

        $orderSql = <<<EOT
select * from __PREFIX__order_settlements where shopId = $shopId
EOT;

        $orderSql .= " order by settlementId";
        $shopSettlementOrderList = $this->query($orderSql);
        $shopSettlementInfo["orderList"] = $shopSettlementOrderList;
        return $shopSettlementInfo;

    }

}